// pages/search/search.js
let app = getApp();
let searchWords;
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    inputwords: "",
    articleNum: 0,
    shopNum: 0,
    goodsNum: 0,
    shut3:"../images/shut3.png",
    shutshow:false,//是否显示X号图片
    inputTxt: '',
    historyList:[{
      id:0,
      name:"痘痘肌肤"
    },
      {
        id: 0,
        name: "痘痘肌肤"
      },
      {
        id: 1,
        name: "面膜"
      },
      {
        id: 2,
        name: "爽肤水"
      },
      {
        id: 3,
        name: "今天的特价商品"
      }, {
        id: 4,
        name: "痘痘肌肤"
      },
      {
        id: 5,
        name: "痘痘肌肤"
      },
      {
        id: 6,
        name: "面膜"
      },
      {
        id: 7,
        name: "爽肤水"
      },
      {
        id: 8,
        name: "今天的特价商品"
      },

    ],
     hotList: [{
      id: 0,
      name: "痘痘肌肤"
    },
    {
      id: 0,
      name: "痘痘肌肤"
    },
    {
      id: 1,
      name: "面膜"
    },
    {
      id: 2,
      name: "爽肤水"
    },
    {
      id: 3,
      name: "今天的特价商品"
    }, {
      id: 4,
      name: "痘痘肌肤"
    },
    {
      id: 5,
      name: "痘痘肌肤"
    },
    {
      id: 6,
      name: "面膜"
    },
    {
      id: 7,
      name: "爽肤水"
    },
    {
      id: 8,
      name: "今天的特价商品"
    },

    ],
    suggestList: [{
      id: 0,
      name: "痘痘肌肤"
    },
    {
      id: 0,
      name: "痘痘肌肤"
    },
    {
      id: 1,
      name: "面膜"
    },
    {
      id: 2,
      name: "爽肤水"
    }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var arr=[];
    //读取历史搜索缓存
    wx.getStorage({
      key: 'historyKeys',
      success(res) {
        console.log("historyKeys")
        console.log(res.data);
        arr.push(res.data)
      }
    })
    //展示历史搜索
  
  },
  //搜索框失去焦点
  searchBlur:function(event){
      console.log(event.detail);
      searchWords=event.detail.value;
      var that=this;
      that.setData({
        searchWords:searchWords
      })
  },
  //搜索框输入时
  searchInput:function(event){
      var that=this;
     
      console.log(event.detail);
      var inputwords=event.detail.value;//搜索框内输入的文字
    if (inputwords){
      that.setData({
        suggestflag:true,
        shutshow:true
      })
    }else{
      that.setData({
        suggestflag: false,
        shutshow: false
      })
    }
      //测试数据
      this.setData({
        inputwords:inputwords,
        articleNum:45,
        shopNum:30,
        goodsNum:50
      })

  },
  /*********点击删除关键字图片********** */
  clearKeywords:function(){
      var that=this;
      that.setData({
        inputwords: '',
        suggestflag: false,
        shutshow: false


      })
  },
  /********点击搜索按钮************ */
  searchBtn:function(e){
    var that=this;
  
    searchWords=that.data.inputwords//访问接口，传值用
   
    var arr = [];
    //读取历史搜索缓存
    wx.getStorage({
      key: 'historyKeys',
      success(res) {
        console.log("读取缓存内容");
        console.log(res.data);
        if(res.data.length>0){
          for(var i=0;i<res.data.length;i++){
            arr.push(res.data[i]);
          }
         
        }
        console.log("arr");
        console.log(arr);
        
      }
    })
    if (searchWords.length > 0) {
      arr.push(searchWords);
      //存缓存
      wx.setStorage({
        key: 'historyKeys',
        data: arr
      })

    }
    
    //请求接口，跳转页面
    wx.navigateTo({
      url: '/pages/searchResult/searchResult',
    })
    
    
      
  },

  /******清除历史记录***********/ 
  clearUp:function(){
    wx.clearStorage();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})